/**
 * Created by DukeCrushIt on 2015/7/30.
 */
class PlayBar extends eui.Component{
    public constructor(){
        super();
        this.skinName = "pet.PlayBarSkin";
        this.y = 65;
        this.name = "playbar";
    }

    public updateDisplay(){
        this.updateMood();
        this.updateSpeed();
        this.img_cur.source = PetMgr.getInstance().curPet.type == Model.GOOD_TYPE_CAT ? "img_cat_lab" : "img_dog_lab";
    }

    private curShowMood:number = 0;
    public updateMood(){
        var pet:Pet = PetMgr.getInstance().curPet;
        if( pet.mood == this.curShowMood) return;
        this.img_pro_1.width = 93 * pet.mood / 100;
        this.group_mood_value.text = pet.mood+"p"+pet.maxMood;
        this.curShowMood = pet.mood;
    }
    public img_add_mood:eui.Image;
    public img_mood:eui.Image;
    public img_pro_0:eui.Image;
    public img_pro_1:eui.Image;
    public group_mood_value:eui.BitmapLabel;
    public img_arrow_left:eui.Image;
    public img_arrow_right:eui.Image;
    public img_cur:eui.Image;
    public img_male:eui.Image;
    public img_txt_bg_1:eui.Image;
    public img_txt_bg_2:eui.Image;
    public img_female:eui.Image;
    public group_speed_cur:eui.BitmapLabel;
    public group_speed_all:eui.BitmapLabel;
    public group_speed:eui.Group;
    public lab_name:eui.Label;
    public group_lv:eui.BitmapLabel;
    public img_offense:eui.Image;
    public img_attack:eui.Image;
    public img_all:eui.Image;
    public hint_mood:eui.Image;
    public group_attr:eui.Group;
    public group_attack:eui.BitmapLabel;
    public group_offense:eui.BitmapLabel;
    public img_detail:eui.Image;
    public total_meng_0:eui.BitmapLabel;
    public group_meng:eui.Group;
    public partAdded(name:string,instance:any){
        super.partAdded(name,instance);
        if( name == "img_mood"){
            this.img_mood = instance;
            this.img_mood.addEventListener(egret.TouchEvent.TOUCH_TAP, this.showBuffPanel, this);
        }else if( name == "img_pro_0"){
            this.img_pro_0 = instance;
            this.img_pro_0.scale9Grid =  new egret.Rectangle(8,8,1,1);
            this.img_pro_0.width = 315;
            this.img_pro_0.cacheAsBitmap = true;
        }else if( name == "img_pro_1"){
            this.img_pro_1 = instance;
            this.img_pro_1.scale9Grid =  new egret.Rectangle(5,5,1,1);
            this.img_pro_1.width = 309;
            this.img_pro_1.cacheAsBitmap = true;
        }
        else if( name == "lab_pet_name"){
            this.lab_name = instance;
        }else if( name == "img_add_mood"){
            this.img_add_mood = instance;
            this.img_add_mood.addEventListener(egret.TouchEvent.TOUCH_TAP, this.showBuffPanel, this);
        }
        else if( name == "group_mood_value"){
            this.group_mood_value = instance;
        }
        else if( name == "img_cur"){
            this.img_cur = instance;

        }else if( name == "group_speed_cur"){
            this.group_speed_cur = instance;
        }else if( name == "group_speed_all"){
            this.group_speed_all = instance;
        }
        else if( name == "img_arrow_left"){
            this.img_arrow_left = instance;
            instance.visible = false;
            this.img_arrow_left.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onPageLeft, this);
        }else if( name == "img_arrow_right"){
            this.img_arrow_right = instance;
            this.img_arrow_right.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onPageRight, this);
        }else if( name == "img_male"){
            this.img_male = instance;
        }else if( name == "img_female"){
            this.img_female = instance;
        }else if( name == "group_lv"){
            this.group_lv = instance;
        }else if( name == "img_txt_bg_1"){
            this.img_txt_bg_1 = instance;
        }else if( name == "img_txt_bg_2"){
            this.img_txt_bg_2 = instance;
        }else if( name == "img_offense"){
            this.img_offense = instance;
        }else if( name == "img_attack"){
            this.img_attack = instance;
        }else if( name == "img_all"){
            this.img_all = instance;
        }else if( name == "group_speed"){
            this.group_speed = instance;
        }else if( name == "group_attr"){
            this.group_attr = instance;
        }else if( name == "group_attack"){
            this.group_attack = instance;
        }else if( name == "group_offense"){
            this.group_offense = instance;
        }else if( name == "img_detail"){
            this.img_detail = instance;
            this.img_detail.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onDetails, this);
        }else if( name == "hint_mood"){
            this.hint_mood = instance;
            RedPointMgr.getInstance().registerPoint(RedPointMgr.MOOD_BUFF,instance);
        }else if( name == "group_meng"){
            this.group_meng = instance;
        }else if( name == "total_meng_0"){
            this.total_meng_0 = instance;
        }
    }

    public updateMeng(){
        this.total_meng_0.text = Model.me.sproutPoint + "";
    }

    private onDetails(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        DecDetailPanel.getInstance().show();
    }
    private curShowAttack;
    private curShowDefense;
    public updateDecorateRelated(){
        var petattr = Model.GetPerAttrs(PetMgr.getInstance().curPet.petId);
        if( petattr){
            var attack = petattr.attack;
            if( this.curShowAttack != attack){
                this.group_attack.text = attack+"";
                this.curShowAttack = attack
            }
            var defense = petattr.defense;
            if( this.curShowDefense != defense){
                this.group_offense.text = defense+"";
                this.curShowDefense = defense
            }
        }else{
            if( this.curShowAttack != 0){
                this.group_attack.text = "0";
                this.curShowAttack = 0
            }
            if( this.curShowDefense != 0){
                this.group_offense.text = "0";
                this.curShowDefense = 0
            }
        }
        this.updateMeng();
    }

    public switchModel(model:number){
        this.group_attr.visible = model == 1;
        this.group_meng.visible = model == 1;
        this.group_speed.visible = model == 0;
    }

    private showBuffPanel(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        if( evt.target == this.img_mood){
            RedPointMgr.getInstance().update(RedPointMgr.MOOD_BUFF,false);
        }
        BuffPanel.getInstance().show(evt.target == this.img_add_mood ? 3 : 1);
    }

    private mySpeed:number;
    private totalSpeed:number;
    public updateSpeed(){
        var temp = PetMgr.getInstance().getTotalHairs();
        if( temp != this.totalSpeed){
            this.group_speed_all.text = temp+"s";
            this.totalSpeed = temp;
        }
        temp = PetMgr.getInstance().curPet.getHair();
        if( temp != this.mySpeed){
            this.group_speed_cur.text = temp+"s";
            this.mySpeed = temp;
        }
    }

    public curPage:number = 0;
    private onPageLeft(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.curPage--;
        this.showPage();
    }

    private onPageRight(evt:egret.TouchEvent){
        SoundMgr.getInstance().playEffect(SoundMgr.BTN);
        this.curPage++;
        this.showPage();
    }
    private curGrade:number;
    public updatePageDisplay(){
        var pet:Pet = PetMgr.getInstance().curPet;
        this.lab_name.text = pet.name;
        this.img_male.visible = pet.sex == Model.MALE;
        this.img_female.visible = pet.sex == Model.FEMALE;
        if( pet.grade != this.curGrade){
            this.group_lv.text = pet.grade+"";
            this.curGrade = pet.grade;
        }
        this.updateMood();
    }

    public setPage(page:number){
        this.curPage = page;
        this.img_arrow_right.visible = PetMgr.getInstance().pets.length - 1 > page;
        this.img_arrow_left.visible = page > 0;
    }

    public showPage(){
        this.setPage(this.curPage);
        PetMgr.getInstance().selectPetByIndex(this.curPage);
        if( this.parent != null)
            this.updateSpeed();
        this.updatePageDisplay();
        if( MainScene.getInstance().parent)
            MainScene.getInstance().updateItems();
        else if( DecorateScene.getInstance().parent)
            DecorateScene.getInstance().pageHandler();
    }

    private static _instance:PlayBar;
    public static getInstance():PlayBar{
        if( PlayBar._instance == null)
            PlayBar._instance = new PlayBar();
        return PlayBar._instance;
    }
}